// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "DBManager/Repository/HDF5/Input/Property/PropertySummaryRepository.h"

#include "DBManager/Repository/HDF5/Base/HDF5RepositoryMacro.h"
#include "DBManager/Repository/HDF5/Base/SingleRepositoryBase.h"

HDF5_REG_REPOSITORY (PropertySummaryRepository)

using namespace SG::DBManager::HDF5;

using namespace SG::DBManager;
using namespace SG::DataStructure::FEM;

// ICoordinate
PropertySummaryRepository::PropertySummaryRepository (HDF5Database& database)
    : SingleRepositoryBase<SummaryInfo<SG::DataStructure::FEM::PropertyTypes>> (database)
{}

void PropertySummaryRepository::InitialDatasetInfo ()
{
    RegisterTableInfo<SummaryInfo<SG::DataStructure::FEM::PropertyTypes>> ("/OWN/INPUT/PROPERTY/SUMMARY/Summary", MainDataset);
    RegisterTableInfo<SG::DataStructure::Common::Id> ("/OWN/INDEX/INPUT/PROPERTY/SUMMARY", IndexDataset);
    CreateDatasetInFile ();
}

PropertyTypes PropertySummaryRepository::GetPropertyType (SG::DataStructure::Common::Id id)
{
    SummaryInfo<SG::DataStructure::FEM::PropertyTypes> PropertyInfo;  // NOLINT(misc-const-correctness)
    if (FindById (id, PropertyInfo) == SG::DataStructure::Common::Status::STATUS_Success)
    {
        return PropertyInfo.m_type;
    }
    throw std::runtime_error ("PID is bad input:" + std::to_string (id) + " Size:" + std::to_string (Size ()));
}

void PropertySummaryRepository::InitDataset ()
{
    SingleRepositoryBase<SummaryInfo<SG::DataStructure::FEM::PropertyTypes>>::InitDataset ();

    auto insertData = [=] () {
        // 触发持久化信号
        GetDatabase ().GetHDF5Hub()->m_propertySummarySignal ();

        // 便利所有index表，进行数据的读取
        std::map<SG::DataStructure::Common::Id, std::set<SG::DataStructure::FEM::ConstraintType>> infos;
        for (auto&& i : GetDatabase ().GetHDF5Hub()->m_propertyTypeWithIndexPathMapping)
        {
            if (GetDatabase ().GetFile ()->exist (i.second))
            {
                std::vector<SG::DataStructure::Common::Id> idList;
                GetDataset (i.second).read (idList);

                std::vector<SG::DBManager::SummaryInfo<SG::DataStructure::FEM::PropertyTypes>> propertySummary;
                propertySummary.reserve (idList.size ());
                for (size_t index = 0; index < idList.size (); index++)
                {
                    //     // 创建数据结构
                    SG::DBManager::SummaryInfo<SG::DataStructure::FEM::PropertyTypes> sum = {
                        idList[index], i.first, static_cast<SG::DataStructure::Common::I64> (index)
                    };
                    propertySummary.push_back (sum);
                }
                // 添加数据
                Add (propertySummary);
            }
        }
    };

    if (GetMainDatasetInfo ().GetRowCount () == 0)
    {
        insertData ();
        SortData ();
    }
}